package com.pzr.soccer.controller;

import com.pzr.soccer.domain.game.Game;
import com.pzr.soccer.domain.game.GameWrapper;
import com.pzr.soccer.service.game.FootballClawerService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.Set;
import java.util.TreeMap;

@RestController
public class TestClawerController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RedisTemplate <String,Object> redisTemplate;
    @Autowired
    FootballClawerService footballClawerService;

    @GetMapping("test/rabbit/liveGame")
    public String sendLiveGameMessage(){

        GameWrapper wrapper=new GameWrapper();
        Game game=new Game();
        game.setPairScore(new TreeMap<>());
        game.getPairScore().put(4,new int[]{4,6});
        game.setId("a test game");
        wrapper.setCurrentMin(45);
        wrapper.setGame(game);
        wrapper.setId("i am a test wrap!");

        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("LiveGameExchange", "LiveGameRouting", wrapper);
        return "ok";
    }

    @GetMapping("/security/user")
    public Object getPrincipal(Principal principal){
        Object principal1 = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return  principal1;
    }

    @GetMapping("test/crawler/redis")
    public Set<Object> testRedis(){
        String setKey="testSet";

      redisTemplate.opsForSet().add(setKey,"v1");
      redisTemplate.opsForSet().add(setKey,"v1");
      redisTemplate.opsForSet().add(setKey,"v2");
      redisTemplate.opsForSet().add(setKey,"v2");
      redisTemplate.opsForSet().add(setKey,"v3");

     Set<Object> values=redisTemplate.opsForSet().members(setKey);

        System.out.println(redisTemplate.opsForSet().members(setKey).contains("v1"));

        redisTemplate.opsForSet().members(setKey).remove("v1");

        System.out.println(redisTemplate.opsForSet().members(setKey).contains("v1"));

        // long size=redisTemplate.opsForSet().size(setKey);

        return values;
    }

   @GetMapping("/test/crawler/history/{deep}")
    public void fetchHistory(@PathVariable(value = "deep")int deep){
   }


    @GetMapping("/test/crawler/exception")
    public String testException(){
       footballClawerService.testException();
       return "ok";
    }

}
